package grafica;


import javax.swing.JPanel;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;


public class MenuPanel extends JPanel implements MouseMotionListener,MouseListener {
	private Toolkit t;
	private Myframe mf;
	private MyLevelPanel livelliEditor;
	private NetPlayPanel deathMatch;
	private MediaTracker mt;
	private Color colNewGame;
	private Color colLoadGame;
	private Color colNetGame;
	private Color colEdiGame;
	private Color colYourGame;
	private Font fontBase;
	private Font myFont;
	private Font commenti;
	
	private float fontDimension;
	private boolean comm1;
	private boolean comm2;
	private boolean comm3;
	private boolean comm4;
	private boolean comm5;
	
	public MenuPanel(Myframe mf) {
		super();
		this.setLayout(new BorderLayout());
		this.setFocusable(true);
		this.addMouseMotionListener(this);
		this.addMouseListener(this);
		//livelliEditor = new MyLevelPanel(mf);
		//deathMatch = new NetPlayPanel(mf);
		comm1 = false;
		comm2 = false;
		comm3 = false;
		comm4 = false;
		comm5 = false;
		t= Toolkit.getDefaultToolkit();
		mt = new MediaTracker(this);
		colNewGame = Color.BLUE;
		colLoadGame = Color.BLUE;
		colNetGame = Color.BLUE;
		colEdiGame = Color.BLUE;
		colYourGame = Color.BLUE;
		fontDimension = 25;
		this.mf = mf;
		mt.addImage(t.getImage("images/Menu/MegaMenu1.gif"),0);
		mt.addImage(t.getImage("images/Menu/MegaMenu6.png"),1);
		mt.addImage(t.getImage("images/Menu/MegaMenu2.gif"),2);
		mt.addImage(t.getImage("images/Menu/MegaMenu4.gif"),3);
		mt.addImage(t.getImage("images/Menu/MegaMenu3.gif"),4);
		mt.addImage(t.getImage("images/Menu/menuTitle.gif"),5);
		mt.addImage(t.getImage("images/Menu/spazio1.gif"),6);
		mt.addImage(t.getImage("images/Menu/MegaMenu7.gif"),7);
		
		try {
			fontBase = Font.createFont(Font.TRUETYPE_FONT, new File("font"+File.separator+"comic.ttf"));
			mt.waitForAll();
		} 
		catch (FontFormatException e) {e.printStackTrace();}
		catch (IOException e) {e.printStackTrace();}
		catch (InterruptedException e) {e.printStackTrace();}
		myFont = fontBase.deriveFont(Font.BOLD, fontDimension);
		commenti = fontBase.deriveFont(Font.ITALIC, fontDimension-5);
		this.requestFocus();
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponents(g);
		g.drawImage(t.getImage("images/Menu/spazio1.gif"),0, 0, null);
		g.drawImage(t.getImage("images/Menu/MegaMenu1.gif"), 50, 20, null);
		g.drawImage(t.getImage("images/Menu/menuTitle.gif"), 80, 440, null);
		g.setColor(colNewGame);
		g.fillRoundRect(420, 70, 240, 70, 20, 30);
		g.setColor(colLoadGame);
		g.fillRoundRect(420, 160, 240, 70, 20, 30);
		g.setColor(colNetGame);
		g.fillRoundRect(420, 250, 240, 70, 20, 30);
		g.setColor(colEdiGame);
		g.fillRoundRect(420, 340, 240, 70, 20, 30);
		g.setColor(colYourGame);
		g.fillRoundRect(420, 430, 240, 70, 20, 30);
		g.setColor(Color.WHITE);
		g.setFont(myFont);
		g.drawString("NUOVA",510 ,100 );
		g.drawString("PARTITA",500 ,130 );
		g.drawString("CARICA",510 ,190 );
		g.drawString("PARTITA",500 ,220 );
		g.drawString("DEATH",500 ,280 );
		g.drawString("MATCH",510 ,310 );
		g.drawString("LEVEL",510 ,370 );
		g.drawString("EDITOR",500 ,400 );
		g.drawString("I TUOI",510 ,460 );
		g.drawString("LIVELLI",500 ,490 );
		g.drawImage(t.getImage("images/Menu/MegaMenu6.png"),430, 70, null);
		g.drawImage(t.getImage("images/Menu/MegaMenu2.gif"),430, 160, null);
		g.drawImage(t.getImage("images/Menu/MegaMenu4.gif"),430, 250, null);
		g.drawImage(t.getImage("images/Menu/MegaMenu3.gif"),430, 340, null);
		g.drawImage(t.getImage("images/Menu/MegaMenu7.gif"),430, 435, null);
		g.setFont(commenti);
		g.setColor(Color.YELLOW);
		if(comm1){
			g.drawString("COMINCIA UNA NUOVA AVVENTURA!", 350, 550);
		}
		else if(comm2){
			g.drawString("RICOMINCIA DA DOVE HAI LASCIATO.", 350, 550);
		}
		else if(comm3){
			g.drawString("SFIDA I TUOI AMICI.", 425, 550);
		}
		else if(comm4){
			g.drawString("CREA IL TUO LIVELLO!", 420, 550);
		}
		else if(comm5){
			g.drawString("GIOCA AI TUOI LIVELLI!", 420, 550);
		}
	}
	
	public void mouseMoved(MouseEvent e) {
		if((e.getX()> 420)&& (e.getX() < 660)&&(e.getY()< 140) && (e.getY()> 70)){
			colNewGame = Color.CYAN;
			colLoadGame = Color.BLUE;
			colNetGame = Color.BLUE;
			colEdiGame = Color.BLUE;
			colYourGame = Color.BLUE;
			comm1 = true;
			
		}
		else if((e.getX()> 420)&& (e.getX() < 660)&&(e.getY()< 230) && (e.getY()> 160)){
			colNewGame = Color.BLUE;
			colLoadGame = Color.CYAN;
			colNetGame = Color.BLUE;
			colEdiGame = Color.BLUE;
			colYourGame = Color.BLUE;
			comm2 = true;
			
		}
		else if((e.getX()> 420)&& (e.getX() < 660)&&(e.getY()< 320) && (e.getY()> 250)){
			colNewGame = Color.BLUE;
			colLoadGame = Color.BLUE;
			colNetGame = Color.CYAN;
			colEdiGame = Color.BLUE;
			colYourGame = Color.BLUE;
			comm3 = true;
			
		}
		else if((e.getX()> 420)&& (e.getX() < 660)&&(e.getY()< 410) && (e.getY()> 340)){
			colNewGame = Color.BLUE;
			colLoadGame = Color.BLUE;
			colNetGame = Color.BLUE;
			colEdiGame = Color.CYAN;
			colYourGame = Color.BLUE;
			comm4 = true;
			
		}
		else if((e.getX()> 420)&& (e.getX() < 660)&&(e.getY()< 5000) && (e.getY()> 430)){
			colNewGame = Color.BLUE;
			colLoadGame = Color.BLUE;
			colNetGame = Color.BLUE;
			colEdiGame = Color.BLUE;
			colYourGame = Color.CYAN;
			comm5 = true;
			
		}
		else
		{
			colNewGame = Color.BLUE;
			colLoadGame = Color.BLUE;
			colNetGame = Color.BLUE;
			colEdiGame = Color.BLUE;
			colYourGame = Color.BLUE;
			comm1 = false;
			comm2 = false;
			comm3 = false;
			comm4 = false;
			comm5 = false;
		}
		this.repaint();
	}
	
	public void mousePressed(MouseEvent e) {
		if((e.getX()> 420)&& (e.getX() < 620)&&(e.getY()< 140) && (e.getY()> 70)){
			mf.nextPanel(null, 0, true);
		}
		else if((e.getX()> 420)&& (e.getX() < 660)&&(e.getY()< 410) && (e.getY()> 340)){
			mf.avviaEditor();
		}
		else if((e.getX()> 420)&& (e.getX() < 660)&&(e.getY()< 5000) && (e.getY()> 430)){
			this.removeAll();
			this.livelliEditor = new MyLevelPanel(mf);
			this.add(livelliEditor,BorderLayout.WEST);			
			this.repaint();
			this.updateUI();
		}
		else if((e.getX()> 420)&& (e.getX() < 660)&&(e.getY()< 230) && (e.getY()> 160)) {
			mf.loadPanel();
		}
		else if((e.getX()> 420)&& (e.getX() < 660)&&(e.getY()< 320) && (e.getY()> 250)){
			this.removeAll();
			this.deathMatch = new NetPlayPanel(mf);
			this.add(deathMatch,BorderLayout.WEST);			
			this.repaint();
			this.updateUI();		
		}
	}
	
	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}
	public void mouseDragged(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}

	public Font getMyFont() {
		return myFont;
	}
	
}
